package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = menuService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id") Integer id) {
        try {
            menuService.deleteById(id);
            return new Result(true, "删除成功");
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, "新增菜单数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增菜单数据失败");
        }
    }

    /**
     * 根据id查询
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id") Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, "回显数据成功", menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显数据失败");
        }
    }

    /**
     * 编辑修改
     * @param menu
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
            return new Result(true, "编辑数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑数据失败");
        }
    }

    @RequestMapping("findMenuByUsername")
    public Result findMenuByUsername(@RequestParam String username) {
        try {
            List<Menu> list = menuService.findMenuByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

}
